package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.model.Result;
import com.woniuxy.controller.form.SetDietTimeConfigForm;
import com.woniuxy.dao.model.DietTimeConfig;
import com.woniuxy.service.DietTimeConfigService;
import com.woniuxy.service.dto.UserDetailsDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 早中晚餐时间配置表 前端控制器
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@RestController
@RequestMapping("/dietTimeConfig")
@Api(tags = "商家设置营业时间操作的接口")
public class DietTimeConfigController {

    @Autowired
    private DietTimeConfigService dietTimeConfigService;

    /**
     * 商家设置营业时间的接口
     * @param sdtcf
     * @return
     */
    @PostMapping("/setDietTimeConfig")
    @ApiOperation("商家设置营业时间的接口")
    public Result setDietTimeConfig(@RequestBody SetDietTimeConfigForm sdtcf){
        UserDetailsDto userDetailsDto = (UserDetailsDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        Long storeId = userDetailsDto.getId();

        DietTimeConfig dtc = dietTimeConfigService.getOne(new QueryWrapper<DietTimeConfig>().eq("storeId",storeId));
        if(dtc != null){
            //修改此营业时间
            dtc.setStartTime(sdtcf.getStartTime());
            dtc.setEndTime(sdtcf.getEndTime());
            dietTimeConfigService.update(dtc,new QueryWrapper<DietTimeConfig>().eq("storeId",storeId));
        }else{
            //添加营业时间
            DietTimeConfig dietTimeConfig = new DietTimeConfig();
            dietTimeConfig.setStoreId(storeId);
            dietTimeConfig.setStartTime(sdtcf.getStartTime());
            dietTimeConfig.setEndTime(sdtcf.getEndTime());
            dietTimeConfigService.save(dietTimeConfig);
        }

        return Result.success(200,"设置营业时间成功",null);
    }

}

